package com.kopi.Sendungsverfolgung;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {
	/** Called when the activity is first created. */

	TextView _tvData;
	TextView _tvHistory;
	Pattern _dataPattern;
	Pattern _historyPattern;
	Pattern _trPattern;
	Pattern _thPattern;
	Pattern _tdPattern;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		InitializePatterns();
		InitializeViews();

		String packageNumber = "1015832002366490270007";
		String url = "http://post.at/sendungsverfolgung.php?language=de&pnum1="
				+ packageNumber;

		String html = Helper.GetHtml(url, true);
		getData(html);
		getHistory(html);

		Log.d("tag", "finished");
	}

	private void getData(String html) {
//		List<String> dataTHs = new ArrayList<String>();
//		List<String> dataTDs = new ArrayList<String>();
		StringBuilder sb = new StringBuilder();

		Matcher dataM = _dataPattern.matcher(html);

		if (dataM.find()) {
			Matcher dataTHM = _thPattern.matcher(dataM.group(1));
			while (dataTHM.find())
				sb.append(dataTHM.group(1) + ", ");
//				dataTHs.add(dataTHM.group(1));

			Matcher dataTDM = _tdPattern.matcher(dataM.group(1));
			while (dataTDM.find())
				sb.append(dataTDM.group(1) + ", ");
//				dataTDs.add(dataTDM.group(1));

			sb.append("\n");
		}
		
		_tvData.setText(sb.toString());

		Log.d("tag", "finished getData()");
	}

	private void getHistory(String html) {
		// List<String> historyTHs = new ArrayList<String>();
		// List<String> historyTDs = new ArrayList<String>();
		StringBuilder sb = new StringBuilder();

		Matcher historyM = _historyPattern.matcher(html);
		if (historyM.find()) {

			Matcher historyTRM = _trPattern.matcher(historyM.group(1));
			while (historyTRM.find()) {
				Matcher dataTHM = _thPattern.matcher(historyTRM.group(1));
				while (dataTHM.find())
					sb.append(dataTHM.group(1) + ", ");
				// historyTHs.add(dataTHM.group(1));

				Matcher dataTDM = _tdPattern.matcher(historyTRM.group(1));
				while (dataTDM.find())
					sb.append(dataTDM.group(1) + ", ");
				// historyTDs.add(dataTDM.group(1));

				sb.append("\n");
			}
		}
		
		_tvHistory.setText(sb.toString());

		Log.d("tag", "finished getHistory()");
	}

	private void InitializeViews() {
		_tvData = (TextView) findViewById(R.id._tvData);
		_tvHistory = (TextView) findViewById(R.id._tvHistory);
	}

	private void InitializePatterns() {
		_dataPattern = Pattern.compile(
				"<table class=\"contentLayer\">(.*?)</table>",
				Pattern.MULTILINE);
		_historyPattern = Pattern.compile(
				"<table class=\"contentLayer print sendung\">(.*?)</table>",
				Pattern.MULTILINE);
		_trPattern = Pattern.compile("<tr>(.*?)</tr>", Pattern.MULTILINE);
		_thPattern = Pattern.compile("<th.*?>(.*?)</th>", Pattern.MULTILINE);
		_tdPattern = Pattern.compile("<td>(.*?)</td>", Pattern.MULTILINE);
	}
}